package SwingView;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import SwingModel.CmsInstructor;
import SwingModel.CmsUser;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.List;


public class CmsCPPeople extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JPanel jpContent;
	
	public CmsCPPeople(){

		//instantiate People title
		jlTitle = new JLabel("People");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(650,40));
		jp.setOpaque(false);
		jp.setLayout( new FlowLayout(FlowLayout.CENTER));
		jp.setBorder(BorderFactory.createEmptyBorder());
		jp.add(jlTitle);
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);

		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);

		//set up student people panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
		
	}
	
	
	/**
     * Set the displaying list of people
     * <p>
     * This method will automatically generate and display a list of
     * people on the People Page.
     *
     * @param     peopleData list of people entries
     */
	public void setPeopleData(List<CmsUser> listOfPeople){
		final Color DARK_GRAY = new Color(180,180,180);
		final Color LIGHT_GRAY = new Color(220,220,220);
		
		jpContent.removeAll();
		for(int i = 0; i<listOfPeople.size(); i++){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			
			CmsUser peopleEntry = listOfPeople.get(i);
			String fullName;
			String role;
			
			//get full name
			fullName = peopleEntry.getFirstName() + " " + peopleEntry.getLastName();
			
			//get role:
			if(peopleEntry instanceof CmsInstructor) role = "Instructor";
			else role = "Student";
			
			if(i%2 == 0){
				jpContent.add(new PeopleEntryRow(fullName , role, DARK_GRAY), c);
			}
			else{
				jpContent.add(new PeopleEntryRow(fullName, role, LIGHT_GRAY), c);
			}
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = listOfPeople.size();
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
	}
	
	private class PeopleEntryRow extends JPanel{
		
		final static private int NAME_COLUMN_SIZE = 400;
		final static private int ROLE_COLUMN_SIZE = 100;
		final static private int ROW_HEIGHT = 25;
		
		public PeopleEntryRow(String fullName, String role, Color color){
			
			
			JLabel fn = new JLabel("   " + fullName);//Name
			fn.setFont(fn.getFont().deriveFont(14.0f).deriveFont(Font.BOLD));
			
			JLabel r = new JLabel(role + "   ");//role
			r.setHorizontalTextPosition(SwingConstants.RIGHT);
			r.setHorizontalAlignment(SwingConstants.RIGHT);
			
			fn.setPreferredSize(new Dimension(NAME_COLUMN_SIZE,ROW_HEIGHT));
			r.setPreferredSize(new Dimension(ROLE_COLUMN_SIZE,ROW_HEIGHT));
			
			setBackground(color);
			setOpaque(true);
			setLayout(new FlowLayout());
			add(fn);
			add(r);
		}
	}
	
	
	//---------------------------------------------------------
	//CLASS: peopleTableModel
	// - table model to initiate the people table
	// - keep track of the column name and row data.
	//---------------------------------------------------------	
	public class PeopleTableModel extends AbstractTableModel{
		private String[] columnNames=new String[]{"Name","Role"};
		private String[][] data;
		
		public PeopleTableModel(){
			data = new String[][]{{"default1","default1"},{"default2","default2"}};
		}
		
		public void setData(String[][] data){
			this.data = data;
		}
		
		@Override
		public int getRowCount() {
			return data.length;
		}

		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}
		
		@Override
		public String getColumnName(int col) {
	        return columnNames[col].toString();
	    }
		
		@Override
		public Class getColumnClass(int c) {
	        return getValueAt(0, c).getClass();
	    }
	}
}
